//
// Ephi - simulation of magnetic fields and particles
// Copyright (C) 2007 Indrek Mandre <indrek(at)mare.ee>
// For more information please see http://www.mare.ee/indrek/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#ifndef __matrix_hpp__
#define __matrix_hpp__

#include <list>
#include <string>
#include <vector>

/** System of linear equations. */
class LinearSystem
{
public:
  /** Constructs a linear system with n unknowns, n equations and k b-values.
    * The result will contain k sets of values. */
  LinearSystem (size_t n, size_t k = 1);
  ~LinearSystem ();

  /** Solve the linear system using Gauss-Jordan elimination technique with partial pivoting. */
  bool solveGJ ();

  /** Solve the linear system using Gauss-Jordan elimination technique with partial pivoting. */
  bool solveGBS ();

  /** Row consists of n coefficents plus k b-values */
  prec_t *getRow (size_t i) { return rows[i]; }
  prec_t **getRows() { return &rows[0]; }
  size_t getN() const { return n; }
  size_t getK() const { return k; }

  const prec_t *getResults (size_t i = 0) const { return results + i * n; }

  void print();

  /** Clear all cells to 0. */
  void clear();

private:
  size_t n, k, npk;
  prec_t *data;
  prec_t *results;
  std::vector<prec_t *> rows;

  LinearSystem (const LinearSystem& copy);
  LinearSystem& operator= (const LinearSystem& copy);
};

#endif // !__potential_hpp__

